package com.bytz.modules.hikvision.config;

import com.bytz.modules.hikvision.Commom.osSelect;
import com.bytz.modules.hikvision.HCNetSDK;
import com.bytz.modules.hikvision.callBack.FMSGCallBack_V31;
import com.sun.jna.Native;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class HikvisionConfiguration {
    public static final String windowsPath = System.getProperty("user.dir") + "\\lib\\windows\\";
    public static final String linuxPath = System.getProperty("user.dir") + "/lib/linux/";

    // 加载HCNetSDK并进行初始化
    @Bean
    public HCNetSDK createSDKInstance() {
        synchronized (HCNetSDK.class) {
            String strDllPath = "";
            HCNetSDK hCNetSDK = null;
            try {
                if (osSelect.isWindows()) {
                    //win系统加载库路径
                    strDllPath = windowsPath + "HCNetSDK.dll";
                    hCNetSDK = (HCNetSDK) Native.loadLibrary(strDllPath, HCNetSDK.class);
                } else if (osSelect.isLinux()) {
                    //Linux系统加载库路径
                    strDllPath = linuxPath +  "libhcnetsdk.so";
                    hCNetSDK = (HCNetSDK) Native.loadLibrary(strDllPath, HCNetSDK.class);

                    HCNetSDK.BYTE_ARRAY ptrByteArray1 = new HCNetSDK.BYTE_ARRAY(256);
                    HCNetSDK.BYTE_ARRAY ptrByteArray2 = new HCNetSDK.BYTE_ARRAY(256);
                    //这里是库的绝对路径，请根据实际情况修改，注意改路径必须有访问权限
                    String strPath1 = linuxPath + "libcrypto.so.1.1";
                    String strPath2 = linuxPath + "libssl.so.1.1";

                    System.arraycopy(strPath1.getBytes(), 0, ptrByteArray1.byValue, 0, strPath1.length());
                    ptrByteArray1.write();
                    hCNetSDK.NET_DVR_SetSDKInitCfg(3, ptrByteArray1.getPointer());

                    System.arraycopy(strPath2.getBytes(), 0, ptrByteArray2.byValue, 0, strPath2.length());
                    ptrByteArray2.write();
                    hCNetSDK.NET_DVR_SetSDKInitCfg(4, ptrByteArray2.getPointer());

                    String strPathCom = linuxPath;
                    HCNetSDK.NET_DVR_LOCAL_SDK_PATH struComPath = new HCNetSDK.NET_DVR_LOCAL_SDK_PATH();
                    System.arraycopy(strPathCom.getBytes(), 0, struComPath.sPath, 0, strPathCom.length());
                    struComPath.write();
                    hCNetSDK.NET_DVR_SetSDKInitCfg(2, struComPath.getPointer());
                } else {
                    throw new RuntimeException("未适配该操作系统");
                }
                // sdk初始化
                hCNetSDK.NET_DVR_Init();
                // 设置报警回调函数
                hCNetSDK.NET_DVR_SetDVRMessageCallBack_V50(0, new FMSGCallBack_V31(), null);
                return hCNetSDK;
            } catch (Exception e) {
                System.out.println();
                throw new RuntimeException("loadLibrary: " + strDllPath + " Error: " + e.getMessage(), e);
            }
        }
    }
}
